<cfparam name="url.vw" default="">
<cfparam name="stimein" default="07:00">
<cfparam name="stimeout" default="16:00">
<cfparam name="shrwrk" default="8">
<cfparam name="sid" default="">
<cfparam name="sDate" default="#Now()#">
<cfparam name="slvcode" default="">
<cfparam name="srecdate" default="#Now()#">
<cfparam name="sreccby" default="">
<cfparam name="scat" default="">
<cfparam name="sguid" default="">
<cfparam name="spatt" default="">

<cfset docid =  url.vw >
<CFSET isOk = true>
<cfset swmsg = "">

<cftransaction action="begin">
	<cftry>
    
    	<cfquery name="getDate" datasource="#client.company_dsn#">
        	 SELECT DISTINCT(REFERENCEDATE) FROM CINWSHISTORY
             WHERE WSDOCNUMBER ='#docid#'
             AND APPROVED IS NULL OR APPROVED ='N'
        </cfquery>
        


        <cfoutput query="getDate">
        
            <cfquery name="getTempData" datasource="#client.company_dsn#" maxrows="1">
                SELECT 
                    ISLEAVE,PATTERNCODE,PERSONNELIDNO,REFERENCEDATE,RECDATECREATED,RECCREATEDBY,CATEGORY,GUID 
                FROM 
                    CINWSHISTORY
                WHERE 
                    WSDOCNUMBER ='#docid#' AND 
                    REFERENCEDATE = <cfqueryparam value="#REFERENCEDATE#" cfsqltype="cf_sql_date"> AND
                    (APPROVED IS NULL OR APPROVED ='N')
                ORDER BY 
                    DATELASTUPDATE DESC, TIMELASTUPDATE DESC
            </cfquery>   
  
            <cfquery name="getLvType" datasource="#client.company_dsn#">
                SELECT LEAVETYPE
                FROM CLKLVCODE
                WHERE DESCRIPTION LIKE '%VACATION%'
            </cfquery>
            
            <cfset sid = getTempData.PERSONNELIDNO>
            <cfset sDate = getTempData.REFERENCEDATE>
            <cfset slvcode = getLvType.LEAVETYPE>
            <cfset srecdate = getTempData.RECDATECREATED>
            <cfset sreccby = getTempData.RECCREATEDBY>
            <cfset scat = getTempData.CATEGORY>
            <cfset spatt = getTempData.PATTERNCODE>
            <cfset sguid = getTempData.GUID>
            
            <cfquery name="getWSCode" datasource="#client.company_dsn#" maxrows="1">
                SELECT TIMEIN, TIMEOUT, REQUIREDDAILYHW AS HRWRK
                FROM CLKWORKSKED WHERE WORKSKEDCODE = '#getTempData.PATTERNCODE#'
            </cfquery>
            <cfif NOT getWSCode.RecordCount>
                <cfquery name="getWSCode" datasource="#client.company_dsn#" maxrows="1">
                    SELECT TIMEIN, TIMEOUT, REQUIREDHW AS HRWRK
                    FROM CLKWSPATTERN WHERE PATTERNCODE = '#getTempData.PATTERNCODE#'
                </cfquery>
            </cfif>
            
            <cfset stimein = getWSCode.TIMEIN>
            <cfset stimeout = getWSCode.TIMEOUT>
            <cfset shrwrk = getWSCode.HRWRK>
        
            <cfif UCASE(getTempData.ISLEAVE) EQ 'Y'>
            	<CFQUERY NAME = "chkD" DATASOURCE="#CLIENT.COMPANY_DSN#">
                    SELECT DOCNUMBER FROM CINLEAVEAPPSD
                    WHERE REFERENCEDATE = <cfqueryparam cfsqltype="cf_sql_date" value="#sDate#">
                </CFQUERY>
                <cfif NOT chkD.RecordCount>
                    <CFQUERY NAME = "INSERTM" DATASOURCE="#CLIENT.COMPANY_DSN#">
                        INSERT INTO CINLEAVEAPPSM (
                        PERSONNELIDNO,
                        STARTINGDATE,
                        ENDINGDATE,
                        LEAVETYPE,
                        APPROVED,
                        LEAVEHOURS,
                        WHOLEDAY,
                        NOOFDAYS,
                        ROUTED,
                        LATEUNDERTIME,
                        DATEACTIONWASDONE,
                        RECCREATEDBY,
                        RECDATECREATED,
                        USERID,
                        DATELASTUPDATE,
                        TIMELASTUPDATE,	
                        APPLICATIONDATE,
                        WITHPAY
                        )
                        VALUES (
                            '#sid#',
                            #CreateODBCDate(sDate)#,
                            #CreateODBCDate(sDate)#,
                            '#slvcode#',
                            'Y',
                            '#shrwrk#',
                            '1',
                            '1',
                            'Y',
                            'NA',
                            #CreateODBCDate(Now())#, 
                            '#sreccby#',
                            #CreateODBCDate(srecdate)#,
                            '#client.userid#',
                            #CreateODBCDate(Now())#,
                            '#timeformat(Now(),"HH:mm:ss")#',
                             #CreateODBCDate(srecdate)#,
                            'Y'
                        )
                    </CFQUERY>
                       
                    <CFQUERY NAME = "INSERTD" DATASOURCE="#CLIENT.COMPANY_DSN#">
                        INSERT INTO CINLEAVEAPPSD (PERSONNELIDNO, REFERENCEDATE, APPROVED, LEAVEHOURS, 
                            WITHPAY,LEAVETYPE,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE
                            ,TIMELASTUPDATE,LATEUNDERTIME) 
                        VALUES(
                            '#sid#',
                             #CreateODBCDate(sDate)#,
                            'Y',
                            '#shrwrk#',
                            'y',
                            '#slvcode#',
                            '#sreccby#',
                            #CreateODBCDate(srecdate)#,
                            '#client.userid#',
                            #CreateODBCDate(Now())#,
                            '#timeformat(Now(),"HH:mm:ss")#',
                            'NA'
                        )
                    </CFQUERY>
                </cfif>
            <cfelse>

				<cfquery name="selD" datasource="#client.company_dsn#" maxrows="1">
                	SELECT PERSONNELIDNO 
                    FROM CINLEAVEAPPSD
                    WHERE 
                    	PERSONNELIDNO = '#sid#' AND
                        REFERENCEDATE  = <cfqueryparam cfsqltype="cf_sql_date" value="#sDate#" >	
                </cfquery>
                <CFIF selD.RecordCount>
                    <cfquery name="delD" datasource="#client.company_dsn#">
                        UPDATE CINLEAVEAPPSD
                        SET APPROVED = 'N'
                        WHERE 
                            PERSONNELIDNO = '#sid#' AND
                            REFERENCEDATE  = <cfqueryparam cfsqltype="cf_sql_date" value="#sDate#" >
                    </cfquery>
                </CFIF>  
                
                <cfquery name="delOld" datasource="#client.company_dsn#" maxrows="1">
                    DELETE FROM CINWSSUBSKED
                    WHERE 
                        PERSONNELIDNO = '#sid#' AND
                        REFERENCEDATE  = <cfqueryparam cfsqltype="cf_sql_date" value="#sDate#" >
                </cfquery>
                <cfquery name="insWS" datasource="#client.company_dsn#">
                    INSERT INTO CINWSSUBSKED (PERSONNELIDNO,REFERENCEDATE,GUID,PATTERNCODE,CATEGORY
                        ,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE)
                    VALUES(
                            '#sid#',
                            #CreateODBCDate(sDate)#,
                            '#sguid#',
                            '#spatt#',
                            '#scat#',
                            '#sreccby#',
                            #CreateODBCDate(srecdate)#,
                            '#client.userid#',
                            #CreateODBCDate(Now())#,
                            '#timeformat(Now(),"HH:mm:ss")#'
                    ) 	
                </cfquery>   		
            </cfif>
        </cfoutput>
        
        <cfquery name="updtHistory" datasource="#client.company_dsn#">
            UPDATE CINWSHISTORY
            SET 
                APPROVED ='Y',
                USERID = '#CLIENT.USERID#',
                DATELASTUPDATE = #CreateODBCDate(Now())#,
                TIMELASTUPDATE = '#TimeFormat(Now(),"HH:MM:SS")#'
            WHERE 
                WSDOCNUMBER ='#docid#'
        </cfquery>
        <cfquery name="updtHistory" datasource="#client.company_dsn#">
            UPDATE CINWSFILE
            SET 
                APPROVED ='Y',
                DATELASTUPDATE = #CreateODBCDate(Now())#,
                TIMELASTUPDATE = '#TimeFormat(Now(),"HH:MM:SS")#'
            WHERE 
                DOCNUMBER ='#docid#'
        </cfquery>
        
    <cfcatch>
    	<cfset isOk = false>
		<CFSET swmsg = "An error occured, Please contact you iBOS/e administrator.<br />">
        <CFSET swmsg = cfcatch.detail>
    </cfcatch>    
	</cftry>
    <cfif isOk>
    	<cftransaction action="commit">
        <CFSET vw = URLEncodedFormat( Encrypt( docid, client.ek ) ) >
        <cflocation url="../manning/index.cfm?vw=#vw#" addtoken="yes">
    <cfelse>
    	<cftransaction action="rollback">
        <div style="margin-left:20px">
            <p style="text-align:left; color:#F00; font-size:12px;">
                An error occured upon processing your request.<br  /><br  />
                <li><cfoutput>#swmsg#</cfoutput></li>
                <br  /><br />
                <a href="javascript:history.back()">Back to edit screen</a>
            </p>
        </div>
    </cfif>
</cftransaction>
        
        